﻿@{
    ViewBag.Title = "Sales List";
    ViewBag.Header = "Sales";
    ViewBag.Caption = "Listing";
    ViewBag.AddNewUrl = Url.Action("Maintenance", "Sales");
    ViewBag.XlsUrl = Url.Action("Export", "Sales", new { type = "Excel" });
    ViewBag.PdfUrl = Url.Action("Export", "Sales", new { type = "Pdf" });
    ViewBag.PrintUrl = Url.Action("Print", "Sales");
}
<div id="listing_Sales">
    <div class="box">
        <div class="box-body table-responsive">
            <table id="dt_Grid" class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>Invoice#</th>
                        <th>Invoiced Date</th>
                        <th>Stockhouse</th>
                        <th>Retailer</th>
                        <th>&nbsp;</th>
                        <th>&nbsp;</th>
                    </tr>
                </thead>
                <tbody id="dt_Rows"></tbody>
            </table>
        </div>
    </div></div>

<script id="dataTable_Grid" type="text/x-jquery-tmpl">
    <tr>
        <td>${InvoiceNumber}</td>
        <td>${InvoiceDate}</td>
        <td>${Stockhouse}</td>
        <td>${Retailer}</td>
        <td>
            <table width="100%">
                <thead>
                    <tr>
                        <td align="left">Product&nbsp;</td>
                        <td align="center">Unit Price&nbsp;</td>
                        <td align="center">Qty&nbsp;</td>
                        <td align="center">Line Total&nbsp;</td>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td colspan="4">
                            <hr />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            {{each(index, value) Products.Product}}
                            ${value}&nbsp;<br />
                            {{/each}}
                        </td>
                        <td align="right">
                            {{each(index, value) Products.UnitPrice}}
                            ${value}&nbsp;<br />
                            {{/each}}
                        </td>
                        <td align="right">
                            {{each(index, value) Products.Qty}}
                            ${value}&nbsp;<br />
                            {{/each}}
                        </td>
                        <td align="right">
                            {{each(index, value) Products.LineTotal}}
                            ${value}&nbsp;<br />
                            {{/each}}
                        </td>
                    </tr>
                    <tr>
                        <td>
                            {{each(index, value) Accessories.Product}}
                            ${value}&nbsp;<br />
                            {{/each}}
                        </td>
                        <td align="right">
                            {{each(index, value) Accessories.UnitPrice}}
                            ${value}&nbsp;<br />
                            {{/each}}
                        </td>
                        <td align="right">
                            {{each(index, value) Accessories.Qty}}
                            ${value}&nbsp;<br />
                            {{/each}}
                        </td>
                        <td align="right">
                            {{each(index, value) Accessories.LineTotal}}
                            ${value}&nbsp;<br />
                            {{/each}}
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <hr />
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="2">
                            Total
                        </td>
                        <td align="right">${TotalQtys}</td>
                        <td align="right">${TotalAmt}</td>
                    </tr>
                </tfoot>
            </table>

        </td>
        <td><a href='@Url.Action("Maintenance")?Id=${InvoiceId}'>Delete</a></td>
    </tr>
</script>

<script type="text/javascript">
    $(document).ready(function () {
        $.getJSON('@Url.Action("GetSales", "Sales")', function (result) {
            if (result != null || result != undefined) {
                if (result.length != undefined && result.length != 0) {
                    for (var i = 0; i < result.length; i++) {
                        for (var j = 0; j < result[i].Products.UnitPrice.length; j++)
                            result[i].Products.UnitPrice[j] = parseFloat(result[i].Products.UnitPrice[j]).toFixed(2);
                        for (var k = 0; k < result[i].Products.UnitPrice.length; k++)
                            result[i].Products.LineTotal[k] = parseFloat(result[i].Products.LineTotal[k]).toFixed(2);

                        for (var j = 0; j < result[i].Accessories.UnitPrice.length; j++)
                            result[i].Accessories.UnitPrice[j] = parseFloat(result[i].Accessories.UnitPrice[j]).toFixed(2);
                        for (var k = 0; k < result[i].Accessories.UnitPrice.length; k++)
                            result[i].Accessories.LineTotal[k] = parseFloat(result[i].Accessories.LineTotal[k]).toFixed(2);

                        result[i].TotalAmt = parseFloat(result[i].TotalAmt).toFixed(2);
                    }
                }
                else {
                    for (var j = 0; j < result.Products.UnitPrice.length; j++)
                        result.Products.UnitPrice[j] = parseFloat(result.Products.UnitPrice[j]).toFixed(2);
                    for (var k = 0; k < result.Products.UnitPrice.length; k++)
                        result.Products.LineTotal[k] = parseFloat(result.Products.LineTotal[k]).toFixed(2);

                    for (var j = 0; j < result[i].Accessories.UnitPrice.length; j++)
                        result[i].Accessories.UnitPrice[j] = parseFloat(result[i].Accessories.UnitPrice[j]).toFixed(2);
                    for (var k = 0; k < result[i].Accessories.UnitPrice.length; k++)
                        result[i].Accessories.LineTotal[k] = parseFloat(result[i].Accessories.LineTotal[k]).toFixed(2);

                    result.TotalAmt = parseFloat(result.TotalAmt).toFixed(2);
                }
                
            }
            $.tmpl($('#dataTable_Grid'), result).appendTo('#dt_Rows');

            $('#dt_Grid').dataTable({
                "bPaginate": true,
                "bLengthChange": true,
                "bFilter": true,
                "bSort": true,
                "bInfo": true,
                "bAutoWidth": false
            });

        });
    });

</script>